<!doctype html>

<html lang="zh-CN">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="shortcut icon" href="/favicon.ico">
  <title>比特校园兼职 - 用户注册</title>
  <!-- 导入CSS -->
  <link href="./dist/css/tabler.min.css?1674944402" rel="stylesheet" />
  <link rel="stylesheet" href="./dist/css/jquery.toast.css">
  <!-- 设置字体 -->
  <!-- <style>
      @import url('https://rsms.me/inter/inter.css');
      :root {
      	--tblr-font-sans-serif: 'Inter Var', -apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif;
      }
      body {
      	font-feature-settings: "cv03", "cv04", "cv11";
      }
    </style> -->
</head>

<body class="d-flex flex-column">
  <!-- 正文 -->
  <div class="page page-center">
    <div class="container container-tight py-4">
      <div class="text-center mb-4">
        <img src="./image/bit-logo01.png" height="50" alt="">
      </div>
      <form id="signUpForm" class="card card-md" autocomplete="off" novalidate>
        <div class="card-body">
          <h2 class="text-center mb-4">用户注册</h2>
          <!-- 姓名 -->
          <div class="mb-3">
            <label class="form-label required">姓名</label>
            <input type="text" class="form-control " placeholder="请输入姓名" name="name" id="name">
            <div class="invalid-feedback">姓名不能为空</div>
          </div>
          <!-- 密码 -->
          <div class="mb-3">
            <label class="form-label required">密码</label>
            <div class="input-group input-group-flat">
              <input type="password" class="form-control" placeholder="请输入密码" autocomplete="off" name="password"
                id="password">
              <span class="input-group-text">
                <a href="javascript:void(0);" class="link-secondary" id="password_a" title="显示密码"
                  data-bs-toggle="tooltip"><!-- Download SVG icon from http://tabler-icons.io/i/eye -->
                  <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
                    stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                    <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                    <path d="M12 12m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0" />
                    <path
                      d="M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7" />
                  </svg>
                </a>
              </span>
              <div class="invalid-feedback">密码不能为空</div>
            </div>
          </div>
          <!-- 确认密码 -->
          <div class="mb-3">
            <label class="form-label required">确认密码</label>
            <div class="input-group input-group-flat">
              <input type="password" class="form-control" placeholder="再次输入密码" autocomplete="off" name="passwordRepeat"
                id="passwordRepeat">
              <span class="input-group-text">
                <a href="javascript:void(0);" class="link-secondary" id="passwordRepeat_a" title="显示密码"
                  data-bs-toggle="tooltip"><!-- Download SVG icon from http://tabler-icons.io/i/eye -->
                  <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
                    stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                    <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                    <path d="M12 12m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0" />
                    <path
                      d="M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7" />
                  </svg>
                </a>
              </span>
              <div class="invalid-feedback">请检查确认密码</div>
            </div>
          </div>
          <!-- 电话号码 -->
          <div class="mb-3">
            <label class="form-label required">电话号码</label>
            <input type="text" class="form-control" placeholder="请输入电话号码" name="phoneNum" id="phoneNum">
            <div class="invalid-feedback">请输入正确的电话号码</div>
          </div>
          <!-- 验证码 -->
          <div class="mb-3">
            <label class="form-label required">验证码</label>
            <div class="input-group">
              <input type="text" class="form-control" placeholder="请输入验证码" name="code" id="code">
              <button class="btn btn-outline-primary" type="button" id="sing_up_btn_code">获取验证码</button>
              <div class="invalid-feedback">验证码不能为空</div>
            </div>
          </div>
          <div class="mb-3">
            <label class="form-check">
              <input type="checkbox" class="form-check-input" id="policy" />
              <span class="form-check-label">同意 <a href="#" tabindex="-1">比特校园兼职使用条款和隐私政策</a>.</span>
            </label>
          </div>
          <div class="form-footer">
            <button type="button" class="btn btn-primary w-100" id="submit">注册</button>
          </div>
        </div>
      </form>
      <div class="text-center text-muted mt-3">
        我已有一个账户? <a href="./sign-in.html" tabindex="-1">登录</a>
      </div>
    </div>
  </div>
</body>
<!-- 导入JS -->
<script src="./dist/js/tabler.min.js"></script>
<script src="./dist/js/jquery-3.6.3.min.js"></script>
<script src="./dist/js/jquery.toast.js"></script>
<script>

$(function () {
  // 发送验证码
  $('#sing_up_btn_code').click(function(){
    // 校验电话号码
    let phoneNum = $('#phoneNum').val();
    if (!phoneNum) {
      $('#phoneNum').addClass('is-invalid');
        return;
    }
    // 电话号码格式 
    // let regex = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
    let regex = new RegExp('^1[3|4|5|6|7|8|9][0-9]{9}$');
    if(!regex.test(phoneNum)) {
      $('#phoneNum').addClass('is-invalid');
      return;
    }

    // 构建发送的数据
    let sendData = {
      targetNum : phoneNum,
      type : 0
    };
    // 调用API发送请求
    $.ajax ({
      type : 'post',
      url : '/verify/send',
      contentType : 'application/json',
      data : JSON.stringify(sendData),
      success : function (respData) {
        if (respData.code == 0) {
          // 提示
          $.toast({
            heading: '提示',
            text: '验证码发送成功, 请查收.',
            icon: 'success'
          });
          // 设置发送按钮样式
          resetConuntDownBtn($('#sing_up_btn_code'), 60);
          } else {
            // 提示
            $.toast({
              heading: '提示',
              text: respData.message,
              icon: 'info'
            });
          }
      },
      statusCode: {
        401: function() {
          $.toast({
            heading: '提示',
            text: respData.message,
            icon: 'warning'
          });
        }
      },
      error : function() {
        $.toast({
            heading: '错误',
            text: '出错了，请联系管理员',
            icon: 'error'
          });
      }
    });
  });

  //  更新发送验证码按钮
  function resetConuntDownBtn (btn, seconds) {
      // 原按钮显示的内容
      let btnHtml = $(btn).html();
      // 设置定时器
      let timer = setInterval(function() {
        if (seconds == 0) {
          // 停止定时器
          clearInterval(timer);
          // 恢复原内容
          $(btn).html(btnHtml);
          // 恢复状态
          $(btn).removeClass('disabled');
        } else {
          // 禁用
          if ($(btn).hasClass("disabled") == false) {
            $(btn).addClass('disabled');
          }
          
          // 剩余秒数
          $(btn).html(seconds + ' 秒后重发');
          seconds--;
        }
      }, 1000);
    }

  // 获取表单并校验
  $('#submit').click(function () {
    let checkForm = true;
    // 校验姓名
    if (!$('#name').val()) {
      $('#name').addClass('is-invalid');
      checkForm = false;
    }
    // 校验密码非空
    if (!$('#password').val()) {
      $('#password').addClass('is-invalid');
      checkForm = false;
    }
    // 校验确认密码非空, 校验密码与重复密码是否相同
    if (!$('#passwordRepeat').val() || $('#password').val() != $('#passwordRepeat').val()) {
      $('#passwordRepeat').addClass('is-invalid');
      checkForm = false;
    }
    // 校验电话
    if (!$('#phoneNum').val()) {
      $('#phoneNum').addClass('is-invalid');
      checkForm = false;
    }
    // 校验验证码
    if (!$('#code').val()) {
      $('#code').addClass('is-invalid');
      checkForm = false;
    }
    // 检验政策是否勾选
    if (!$('#policy').prop('checked')) {
      $('#policy').addClass('is-invalid');
      checkForm = false;
    }
    // 根据判断结果提交表单
    if (!checkForm) {
      return false;
    }
    
    // 构造数据
    let sendData = {
      code: $('#code').val(),
      name: $('#name').val(),
      password: $('#password').val(),
      phoneNum: $('#phoneNum').val(),
      repeatPassword: $('#passwordRepeat').val()
    };
    // 发送AJAX请求 
    // contentType = application/json
    // 成功后跳转到 sign-in.html
    $.ajax ({
      type: 'post',
      url: '/user/register',
      contentType: 'application/json',
      data: JSON.stringify(sendData),
      success : function (respData) {
        if (respData.code == 0) {
            // 跳转到登录页
            location.assign('sign-in.html');
          } else {
            // 提示
            $.toast({
              heading: '提示',
              text: respData.message,
              icon: 'info'
            });
          }
      },
      statusCode: {
        401: function() {
          $.toast({
            heading: '提示',
            text: respData.message,
            icon: 'warning'
          });
        }
      },
      error : function() {
        $.toast({
            heading: '错误',
            text: '出错了，请联系管理员',
            icon: 'error'
          });
      }
    });
  });

  // 表单元单独检验
  $('#name, #phoneNum, #password, #code').on('blur', function () {
    if ($(this).val()) {
      $(this).removeClass('is-invalid');
      $(this).addClass('is-valid');
    } else {
      $(this).removeClass('is-valid');
      $(this).addClass('is-invalid');
    }
  })

  // 检验确认密码
  $('#passwordRepeat').on('blur', function () {
    if ($(this).val() && $(this).val() == $('#password').val()) {
      $(this).removeClass('is-invalid');
      $(this).addClass('is-valid');
    } else {
      $(this).removeClass('is-valid');
      $(this).addClass('is-invalid');
    }
  })

  // 校验政策是否勾选
  $('#policy').on('change', function () {
    if ($(this).prop('checked')) {
      $(this).removeClass('is-invalid');
      $(this).addClass('is-valid');
    } else {
      $(this).removeClass('is-valid');
      $(this).addClass('is-invalid');
    }
  })


  // 密码框右侧明文密文切换按钮
  $('#passwordRepeat_a').click(function () {
    if($('#passwordRepeat').attr('type') == 'password') {
      $('#passwordRepeat').attr('type', 'text');
    } else {
      $('#passwordRepeat').attr('type', 'password');
    }
  });
  $('#password_a').click(function () {
    if($('#password').attr('type') == 'password') {
      $('#password').attr('type', 'text');
    } else {
      $('#password').attr('type', 'password');
    }
  });
});


</script>

</html>